package io.zhifei.modules.sys.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.zhifei.common.utils.ApiResponse;
import io.zhifei.modules.app.login.annotation.LoginUser;
import io.zhifei.modules.sys.system.entity.ProblemEntity;
import io.zhifei.modules.sys.system.entity.SysUserEntity;
import io.zhifei.modules.sys.system.service.SysProblemService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p> 常见问题管理 </p>
 *
 * @author： WangPeng <br/>
 * @date： 2020/10/12 10:22 <br/>
 * @version： <br/>
 */
@RestController
@RequestMapping("/sys/pro")
@Api(description = ("常见问题管理"))
public class SysProblemController {

    @Autowired
    private SysProblemService problemService;

    /**
     * 所有问题列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("sys:pro:list")
    @ApiOperation("所有问题列表")
    public ApiResponse<Object> list(Integer page,
                                    Integer pageSize) {
        return new ApiResponse<>(problemService.queryList(page, pageSize));
    }

    @PostMapping("save")
//    @RequiresPermissions("sys:pro:save")
    @ApiOperation("新增常见问题")
    public ApiResponse<Object> save(@RequestBody ProblemEntity problemEntity) {
        try {
            Long userId = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId();
            problemEntity.setCreateUser(String.valueOf(userId));
            problemService.insert(problemEntity);
            return new ApiResponse<>("200", "新增成功！");
        } catch (Exception e) {
            return new ApiResponse<>("500", "新增失败！");
        }
    }

    @PostMapping("update")
//    @RequiresPermissions("sys:pro:update")
    @ApiOperation("修改常见问题")
    public ApiResponse<Object> update(@RequestBody ProblemEntity problemEntity) {
        try {
            problemService.update(problemEntity);
            return new ApiResponse<>("200", "修改成功！");
        } catch (Exception e) {
            return new ApiResponse<>("500", "修改失败！");
        }
    }

    @PostMapping("delete")
//    @RequiresPermissions("sys:pro:update")
    @ApiOperation("删除常见问题")
    public ApiResponse<Object> delete(@RequestBody String[] ids) {
        try {
            List<String> idList = Arrays.asList(ids);
            problemService.delete(idList);
            return new ApiResponse<>("200", "删除成功！");
        } catch (Exception e) {
            return new ApiResponse<>("500", "删除失败！");
        }
    }

    @GetMapping("questionFeedback")
//    @RequiresPermissions("sys:pro:update")
    @ApiOperation("意见反馈")
    public ApiResponse<Object> questionFeedback(Integer page,
                                                Integer pageSize) {
        return new ApiResponse<>(problemService.questionFeedback(page, pageSize));
    }
}
